﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.UpdateServices.Administration;
using WsusWebAdmin.WsusWrappers;
using Microsoft.UpdateServices.Internal;
using System.Net;
using Microsoft.UpdateServices.Internal.BaseApi;
using Microsoft.UpdateServices.Internal.DatabaseAccess;

namespace WsusWebAdmin.Web.Wsus
{
    internal static class UpdateServerFactory
    {
        internal static object GetInstance(NetworkCredential credentials, WsusHostData host)
        {           
            var dataProxy = new AuthenticatedAdminDataAccessProxy(host.RemotingUrl,
                                                                  credentials);

            ClassFactory.SetActivator(typeof(AdminDataAccess), dataProxy);                                                           

            AdminProxy adminProxy = new AdminProxy();
            Uri uri = new Uri(host.RemotingUrl);
            return adminProxy.GetRemoteUpdateServerInstance(uri.Host, host.RemotingUrl.StartsWith("https", true, null), uri.Port); 
        }
    }
}